<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属军区" prop="theirMilitary">
        <el-select
          v-model="queryParams.theirMilitary"
          placeholder="请选择所属军区"
          @change="handleQuery"
        >
          <el-option
            v-for="dict in belong_military_region"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="门店" prop="shopName">
        <el-input
          v-model="queryParams.shopName"
          placeholder="门店"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="选择老师" prop="teacherId">
        <el-select
          clearable
          @change="handleQuery"
          v-model="queryParams.teacherId"
          placeholder="选择老师"
        >
          <el-option
            v-for="dict in hairFixTeacherList"
            :key="dict.id"
            :label="dict.nickName"
            :value="dict.id"
            @change="handleQuery"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['business:hairFixTeacherPhoto:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:hairFixTeacherPhoto:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:hairFixTeacherPhoto:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['business:hairFixTeacherPhoto:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Upload"
          @click="massUpload"
        >批量上传作品文件</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="hairFixTeacherPhotoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="" align="center" prop="id" v-if="true"/>-->
      <el-table-column label="军区" align="center" prop="theirMilitary"/>
      <el-table-column label="门店" align="center" prop="shopName"/>
      <el-table-column label="补发师名字" align="center" prop="teacherName"/>
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="作品类型" align="center" prop="photoType" />
      <el-table-column label="作品内容" align="center" prop="content" />
      <el-table-column label="作品文件" align="center" prop="photoFile">
        <template #default="scope">
          <template v-if="scope.row.photoFile != null && scope.row.photoType == 'img'">
            <image-preview
              :src="scope.row.photoFile"
              style="max-width: 100px; max-height: 100px"
            />
          </template>
          <template v-else="scope.row.photoFile != null">
            <video
              controls
              muted
              :width="200" :height="200"
              type="video/mp4" :src="scope.row.photoFile">
            </video>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="sys_show_hide" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['business:hairFixTeacherPhoto:edit']">修改</el-button>
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['business:hairFixTeacherPhoto:remove']">删除</el-button>
          </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog title="批量上传老师作品" v-model="uploadOpen" width="800px" append-to-body>
      <el-form ref="hairFixTeacherPhotoRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属军区" prop="theirMilitary">
          <el-select
            v-model="form.theirMilitary"
            placeholder="请选择所属军区"
            @change="selectTheirMilitary(form.theirMilitary)"
          >
            <el-option
              v-for="dict in belong_military_region"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择老师" prop="teacherIds">
          <el-select
            clearable
            filterable
            multiple
            v-model="form.teacherIds"
            placeholder="选择老师"
          >
            <el-option
              v-for="dict in hairFixTeacherList"
              :key="dict.id"
              :label="dict.nickName"
              :value="dict.id"
              @change="handleQuery"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="作品文件" prop="photoFile">
          <image-uploads v-model="form.teacherPhotos"></image-uploads>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitUploads">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 添加或修改补发师作品对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body @close="cancel">
      <el-form ref="hairFixTeacherPhotoRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属军区" prop="theirMilitary">
          <el-select
            v-model="form.theirMilitary"
            placeholder="请选择所属军区"
            @change="selectTheirMilitary(form.theirMilitary)"
          >
            <el-option
              v-for="dict in belong_military_region"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="门店" prop="shopId">
          <el-select
            clearable
            v-model="form.shopId"
            placeholder="选择门店"
            @change="selectShop(form.shopId)"
          >
            <el-option
              v-for="dict in hairFixShopList"
              :key="dict.id"
              :label="dict.shopName"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择老师" prop="teacherId">
          <el-select
            clearable
            v-model="form.teacherId"
            placeholder="选择老师"
          >
            <el-option
              v-for="dict in hairFixTeacherList"
              :key="dict.id"
              :label="dict.nickName"
              :value="dict.id"
              @change="handleQuery"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="作品内容" prop="content">
          <el-input type="textarea" v-model="form.content"></el-input>
        </el-form-item>
        <el-form-item label="作品类型" prop="photoType">
          <el-select v-model="form.photoType" placeholder="请选择店铺类型">
            <el-option
              v-for="dict in sys_file_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
              @change="handleQuery"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.photoType === 'img'" label="作品文件" prop="photoFile">
          <image-uploads v-model="form.photoFile"></image-uploads>
        </el-form-item>
        <el-form-item v-else label="作品文件" prop="photoFile">
          <video-upload v-model="form.photoFile"></video-upload>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in sys_show_hide"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="HairFixTeacherPhoto">
import {
  listHairFixTeacher
} from "@/api/business/hairFixTeacher";
import {
  listHairFixShop
} from "@/api/business/hairFixShop";
import {
  listHairFixTeacherPhoto,
  getHairFixTeacherPhoto,
  delHairFixTeacherPhoto,
  addHairFixTeacherPhoto,
  updateHairFixTeacherPhoto,
  massUploadTeacherPhotos
} from "@/api/business/hairFixTeacherPhoto";
import ImageUploads from "@/components/ImageUploads/index.vue";
import VideoUpload from "@/components/VideoUpload/index.vue";
import {useRoute, useRouter} from "vue-router";
const router = useRouter();
const route = useRoute()
const { proxy } = getCurrentInstance();
const { sys_file_type,sys_show_hide,belong_military_region } = proxy.useDict("sys_file_type","sys_show_hide","belong_military_region");
const hairFixTeacherPhotoList = ref([]);
const hairFixTeacherList = ref([]);
const hairFixShopList = ref([]);
const open = ref(false);
const uploadOpen = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const teacherPhotos = ref("");
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    teacherId: route.query != undefined ? route.query?.teacherId : undefined,
    teacherIds: [],
    title: undefined,
    photoType: undefined,
    photoFile: undefined,
    content: undefined,
    status: undefined
  },
  rules: {
    id: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    teacherId: [
      { required: true, message: "补发师ID不能为空", trigger: "blur" }
    ],
    photoType: [
      { required: true, message: "作品类型不能为空", trigger: "change" }
    ],
    photoFile: [
      { required: true, message: "作品文件地址不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "状态(0:显示 1:隐藏)不能为空", trigger: "change" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询补发师作品列表 */
function getList() {
  loading.value = true;
  listHairFixTeacherPhoto(queryParams.value).then(response => {
    hairFixTeacherPhotoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function selectTheirMilitary(row){
  getShopList(row)
  getTeacherList(row)
  form.value.shopId = null;
  form.value.teacherId = null;
  form.value.teacherIds = [];
}

function selectShop(row){
  getTeacherList("",row)
  form.value.teacherId = null;
}
/** 查询补发师信息列表 */
function getTeacherList(theirMilitary,shopId) {
  listHairFixTeacher({
    pageNum: 1,
    pageSize: 999999,
    theirMilitary: theirMilitary,
    shopId:shopId
  }).then((response) => {
    hairFixTeacherList.value = response.rows;
  });
}
function getShopList(theirMilitary) {
  listHairFixShop({
    pageNum: 1,
    pageSize: 999999,
    theirMilitary: theirMilitary}).then((response) => {
    hairFixShopList.value = response.rows;
  });
}
getShopList();
getTeacherList();
// 取消按钮
function cancel() {
  open.value = false;
  uploadOpen.value = false;
  reset();
  getShopList();
  getTeacherList();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    teacherId: null,
    title: null,
    photoType: "img",
    teacherPhotos: null,
    photoFile: null,
    content: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delFlag: null,
    status: '0'
  };
  proxy.resetForm("hairFixTeacherPhotoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  // router.push({query: {},});
  queryParams.value.teacherId = undefined;
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加补发师作品";
}

function massUpload(){
  uploadOpen.value = true;
}

/** 修改按钮操作 */
function handleUpdate(row) {
  loading.value = true
  reset();
  const _id = row.id || ids.value
  getHairFixTeacherPhoto(_id).then(response => {
    loading.value = false;
    form.value = response.data;
    form.value.status = form.value.status + '';
    open.value = true;
    title.value = "修改补发师作品";
  });
}

function submitUploads(){
  massUploadTeacherPhotos(form.value).then(response => {
    proxy.$modal.msgSuccess("上传成功");
    uploadOpen.value = false;
    form.value.teacherPhotos = null;
    form.value.teacherIds = [];
    getList();
  }).finally(() => {
    buttonLoading.value = false;
  });

}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["hairFixTeacherPhotoRef"].validate(valid => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id != null) {
        updateHairFixTeacherPhoto(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      } else {
        addHairFixTeacherPhoto(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除补发师作品编号为"' + _ids + '"的数据项？').then(function() {
    loading.value = true;
    return delHairFixTeacherPhoto(_ids);
  }).then(() => {
    loading.value = true;
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('business/hairFixTeacherPhoto/export', {
    ...queryParams.value
  }, `hairFixTeacherPhoto_${new Date().getTime()}.xlsx`)
}

getList();
</script>
